package org.zjt.rpc.netty.server;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import org.zjt.helloworld.domain.RequestMsgProto;
import org.zjt.helloworld.domain.ResponseMsgProto;
import org.zjt.rpc.domain.RpcMsgProto;
import org.zjt.rpc.util.InvokeMethodExecuter;

/**
 * Created by zhangjuntao on 2017/3/16.
 */
public class RpcServerHandler extends ChannelHandlerAdapter {



    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        RpcMsgProto.msg req = (RpcMsgProto.msg) msg;
        System.out.println("Service accept client subscribe req : [" + req.toString() + "]");
        ctx.writeAndFlush(InvokeMethodExecuter.exec(req));
    }





    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();// 发生异常，关闭链路
    }
}
